<html>
<head>
<title> jquery完美实现textarea输入框限制字数</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<script type="text/javascript" src="jquery-1.9.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(e) {
    $("#weibo").keyup(function() {
		var len = $(this).val().replace(/(^\s*)|(\s*$)/g, "");
		var len2 = len.length;
		if (len2 > 20) {
			$("#word").text('0');
			console.log(len2);
			$(this).val(len.substring(0, 20));
			$(this).attr('disabled', 'disabled');
			if (confirm('输入已达上限，是否重新输入')) {
				$('#weibo').removeAttr("disabled");
			}
		}else{
			var num = 20 - len2;
			$("#word").text(num);		
		}
	});
});
</script>
<style type="text/css">
h6{color:blue;}
textarea{resize:none;}
#word{color:red;}
</style>
</head>
<body>
<h6>说点什么吧，你可以输入<span>20</span>个字，现在剩余<span id="word">20</span>个</h6>
<textarea name="con" id="weibo" cols="45" rows="6" ></textarea>
</body>
</html>
